package descentFly.app
{
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import descentFly.df_utils;
	
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class df_block extends df_component {
		public static const DEFAULT_BACK: Object = {
			psize		: [3, 3],
			spot		: 0.5,
			padding		: 0,
			tileFill	: { 
				colors	: [0x555555, 0x666666]
			},
			ofx			: 0,
			ofy			: 0
		};
		
		public function df_block(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, a_styleClass: Object ='block') {
			super(parent, ax, ay, awidth, aheight, a_styleClass);
		}
		
		private function paintBack(rect: Rectangle): void {
			var back: Object = uif_util.union(DEFAULT_BACK, style.back);
			var y: int = 0;
			
			draw_rect(rect.x, rect.y, rect.width, rect.height, style);
			graphics.endFill();
			graphics.lineStyle(undefined);
			
			applyFillStyle(back.tileFill);
			
			rect.inflate(-back.padding, -back.padding);
			while (y <= rect.height) {
				var gs: Point = new Point(back.psize[0] * back.spot, back.psize[1] * back.spot);
				if (y + gs.y > rect.height ) gs.y = rect.height - y;
				
				var x: int = 0;
				while (x <= rect.width) {
					if (x + gs.x > rect.width ) gs.x = rect.width - x;
					graphics.drawRect(rect.x + x + (y % 2) * gs.x * back.ofx, rect.y + y + (x % 2) * gs.y * back.ofy, gs.x, gs.y);
					x += back.psize[0];
				}
				y += back.psize[1];
			}
			graphics.endFill();
		}
		
		public function tileBack(x: int, y: int, width: int, height: int, a_style: Object):void {
			paintBack(new Rectangle(x, y, width, height));
		}
		
		public function childToCenter(): void {
			var cwidth	: int = 0;
			var child	: uif_component;
			var i		: int;
			for (i=0; i<numChildren; i++) {
				child = getChildAt(i) as uif_component;
				cwidth 	+= child.width;
			}
			
			var p: int = (width - cwidth)/2; 
			for (i=0; i<numChildren; i++) {
				child = getChildAt(i) as uif_component;
				child.x = p;
				p += child.width;
			}			
		}
	}
}